﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using SongSharp.Domain.Objects;
using SongSharp.Appliance;
using SongSharp.Presentation.View;
using SongSharp.Presentation.Command;

namespace SongSharp.Presentation.ViewModel
{
    public class StatisticViewModel : ViewModelBase
    {
        private StatisticView StatisticView { get; set; }
        private PrimeViewModel PrimeModel { get; set; }
        private Window Owner { get; set; }

        private string m_trackInfo { get; set; }
        private string m_statisticInfo { get; set; }

        public string TrackInfo
        {
            get { return m_trackInfo; }
        }

        public string StatisticInfo
        {
            get { return m_statisticInfo; }
        }

        public StatisticViewModel(PrimeViewModel primeModel, Window owner)
        {
            PrimeModel = primeModel;
            Owner = owner;
        }

        public void ShowWindow(Track track)
        {
            string lastPlayed = "Never";
            string lastSkipped = "Never";

            if (track.Statistic.GetLastAction(TrackHandlingType.PlayedTrackToEnd) != null)
                lastPlayed = ((DateTime)track.Statistic.GetLastAction(TrackHandlingType.PlayedTrackToEnd).Date).ToString()
                    + " (" + string.Format("{0:F2}",(DateTime.Now - ((DateTime)track.Statistic.GetLastAction(TrackHandlingType.PlayedTrackToEnd).Date)).TotalHours) + " hours ago)";
            if (track.Statistic.GetLastAction(TrackHandlingType.SkippedTrack) != null)
                lastSkipped = ((DateTime)track.Statistic.GetLastAction(TrackHandlingType.SkippedTrack).Date).ToString()
                     + " (" + string.Format("{0:F2}",(DateTime.Now - ((DateTime)track.Statistic.GetLastAction(TrackHandlingType.SkippedTrack).Date)).TotalHours) + " hours ago)";

            m_trackInfo = System.IO.Path.GetFileNameWithoutExtension(track.Url);
            m_statisticInfo = "Times played: " + track.Statistic.PlayedExplicitCount
                + "\nTimes skipped: " + track.Statistic.SkippedCount
                + "\nPlayed-skipped-ratio: " + ((track.Statistic.PlayedExplicitCount + 1) / (track.Statistic.SkippedCount + 1))
                + "\nDate added: " + track.Statistic.DateAdded + " (" + string.Format("{0:F2}", (DateTime.Now - (DateTime)track.Statistic.DateAdded).TotalDays) + " days ago)"
                + "\nLast played: " + lastPlayed
                + "\nLast skipped: " + lastSkipped
                + "\nVVV: " + SuggestionApp.SuggestionManager.GenerateVaryVogueValue(track).Value.ToString();

            if (StatisticView == null || !StatisticView.IsLoaded)
            {
                StatisticView = new StatisticView();
                StatisticView.DataContext = this;
                StatisticView.Owner = Owner;
                StatisticView.Show();
            }
            else
            {
                StatisticView.Activate();
            }

        }
    }
}
